<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="http://47.100.102.177:8080/app/images/admin/管理.png" type="image/x-icon" />
    <link rel="stylesheet" href="http://47.100.102.177:8080/app/css/admin/login.css">
    <link rel="stylesheet" href="http://47.100.102.177:8080/app/css/common/bootstrap.min.css">
    <script src="http://47.100.102.177:8080/app/js/common/jquery.min.js"></script>
    <script type="text/javascript" src="http://47.100.102.177:8080/app/js/common/bootstrap.min.js"></script>
    <script src="http://47.100.102.177:8080/app/js/common/jquery-1.12.4.js"></script>
    <title>云上书屋管理端登录</title>
</head>

<body>
    <div id="login">
        <h2>云上书屋</h2>
        <h3>管理端</h3>
        <form action="admin.jsp" id="login_form">
            <div class="form-group">
                <label class="call">账号(手机号)</label>
                <label class="tip tip1 one">账号格式错误,必须是位手机号</label>
                <label class="tip tip1 one_1">账号不能为空</label>
                <label class="tip tip1 one_2">该账号不存在</label>
                <input type="text" name="account" class="form-control" id="exampleInputAccount1" placeholder="请输入账号(测试数据:18936093161)">
            </div>
            <div class="form-group">
                <label class="call">密码</label>
                <label class="tip tip2 two">密码格式错误,必须是6~18位,字母开头,数字,下划线</label>
                <label class="tip tip2 two_1">密码不能为空</label>
                <label class="tip tip2 two_2">密码错误</label>
                <input type="password" name="password" class="form-control input_password" id="exampleInputPassword1" placeholder="请输入密码(测试数据:wjz110032)">
            </div>
            <div class="form-group">
                <label class="call">验证码</label>
                <label class="tip three">验证码错误</label>
                <label class="tip three_1">验证码不能为空</label>
                <div class="yzm"><input type="text" value="" placeholder="请输入验证码（不区分大小写）" class="form-control input-val"></div>
                <canvas id="canvas"></canvas>
            </div>
            <button type="button" class="btn btn-default">登录</button>
        </form>
    </div>
    <script>
        $(function() {
                var show_num = [];
                draw(show_num);
                $("#canvas").on('click', function() {
                    draw(show_num);
                })
                $(".btn").on('click', function() {
                    var flag = true;
                    let param = $("#login_form").serialize();
                    let test_phone = '';
                    let test_password = '';
                    $.post('/boot/admin/logincl',param,function(res){
                        let session = res.data;
                        if(res.code=='200'){
                            test_phone = document.getElementById("exampleInputAccount1").value;
                            test_password = document.getElementById("exampleInputPassword1").value;

                        }else if(res.code=='404'){
                            test_password = document.getElementById("exampleInputPassword1").value;
                        }else if(res.code=='500'){
                            test_phone = document.getElementById("exampleInputAccount1").value;
                        }

                        //手机号码格式
                        var account_reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
                        var account = document.getElementById("exampleInputAccount1").value;
                        console.log(account)
                        if (account == '') {
                            $(".tip1").css("display", "none");
                            $(".one_1").css("display", "block");
                            flag = false;
                        } else if (test_phone != account) {
                            $(".tip1").css("display", "none");
                            $(".one_2").css("display", "block");
                            flag = false;
                        } else if (account_reg.test(account)) {
                            $(".one").css("display", "none");
                            $(".one_1").css("display", "none");
                        } else {
                            $(".tip1").css("display", "none");
                            $(".one").css("display", "block");
                            flag = false;
                        }

                        //密码
                        var password_reg = /^[a-zA-Z]\w{5,17}$/;
                        var password = document.getElementById("exampleInputPassword1").value;
                        if (password == '') {
                            $(".tip2").css("display", "none");
                            $(".two_1").css("display", "block");
                            flag = false;
                        } else if (password != test_password) {
                            $(".tip2").css("display", "none");
                            $(".two_2").css("display", "block");
                            flag = false;
                        } else if (password_reg.test(password)) {
                            $(".tip2").css("display", "none");
                        } else {
                            $(".tip2").css("display", "none");
                            $(".two").css("display", "block");
                            flag = false;
                        }



                        var val = $(".input-val").val().toLowerCase();
                        var num = show_num.join("");
                        if (val == '') {
                            $(".three").css("display", "none");
                            $(".three_1").css("display", "block");
                            flag = false;
                        } else if (val == num) {
                            $(".three").css("display", "none");
                            $(".three_1").css("display", "none");
                            $(".input-val").val('');
                            // draw(show_num);
                        } else {
                            $(".three_1").css("display", "none");
                            $(".three").css("display", "block");
                            $(".input-val").val('');
                            flag = false;
                            // draw(show_num);
                        }

                        if (flag) {
                            //传输账号
                            window.location.href = '/boot/admin/admin?' + 'account=' + account;
                        }
                    })
                })
            })
            //生成并渲染出验证码图形
        function draw(show_num) {
            var canvas_width = $('#canvas').width();
            var canvas_height = $('#canvas').height();
            var canvas = document.getElementById("canvas"); //获取到canvas的对象，演员
            var context = canvas.getContext("2d"); //获取到canvas画图的环境，演员表演的舞台
            canvas.width = canvas_width;
            canvas.height = canvas_height;
            var sCode = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
            var aCode = sCode.split(",");
            var aLength = aCode.length; //获取到数组的长度
            for (var i = 0; i < 4; i++) { //这里的for循环可以控制验证码位数（如果想显示6位数，4改成6即可）
                var j = Math.floor(Math.random() * aLength); //获取到随机的索引值
                // var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
                var deg = Math.random() - 0.5; //产生一个随机弧度
                var txt = aCode[j]; //得到随机的一个内容
                show_num[i] = txt.toLowerCase();
                var x = 10 + i * 20; //文字在canvas上的x坐标
                var y = 20 + Math.random() * 8; //文字在canvas上的y坐标
                context.font = "bold 23px 微软雅黑";
                context.translate(x, y);
                context.rotate(deg);
                context.fillStyle = randomColor();
                context.fillText(txt, 0, 0);
                context.rotate(-deg);
                context.translate(-x, -y);
            }
            for (var i = 0; i <= 5; i++) { //验证码上显示线条
                context.strokeStyle = randomColor();
                context.beginPath();
                context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.stroke();
            }
            for (var i = 0; i <= 30; i++) { //验证码上显示小点
                context.strokeStyle = randomColor();
                context.beginPath();
                var x = Math.random() * canvas_width;
                var y = Math.random() * canvas_height;
                context.moveTo(x, y);
                context.lineTo(x + 1, y + 1);
                context.stroke();
            }
        }
        //得到随机的颜色值
        function randomColor() {
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            return "rgb(" + r + "," + g + "," + b + ")";
        }
    </script>
</body>

</html>